package com.yykj.hospital.server.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.common.enums.HospitalAdminType;
import com.yykj.common.vo.PageData;
import com.yykj.hospital.client.dto.*;
import com.yykj.hospital.client.form.HospitalAdminListForm;
import com.yykj.hospital.client.form.HospitalEditForm;
import com.yykj.hospital.client.form.HospitalListForm;
import com.yykj.hospital.client.form.HospitalSettingForm;
import com.yykj.hospital.client.form.HospitalAdminEditForm;
import com.yykj.hospital.server.form.HospitalDepartmentSearchFrom;
import com.yykj.hospital.server.service.DepartmentService;
import com.yykj.hospital.server.service.HospitalAdminService;
import com.yykj.hospital.server.service.HospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/proto/backstage/hospital")
@Api(tags = "内部使用-医院相关")
public class ProtoHospitalBackstageController {

    @GetMapping("/info/{id}")
    @ApiOperation(value = "医院信息")
    public HospitalDTO info(@PathVariable(name = "id") Integer id) {
        return hospitalService.getHospitalInfo(id);
    }

    @GetMapping("/list")
    @ApiOperation(value = "医院列表")
    public PageData<HospitalDTO> getHospitalList(
            @RequestParam(name = "currentPage",defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(name = "name",required = false) String name ) {

        HospitalListForm hospitalListForm = new HospitalListForm();
        hospitalListForm.setCurrentPage(currentPage);
        hospitalListForm.setPageSize(pageSize);
        hospitalListForm.setName(name);
        return  hospitalService.getHospitalList(hospitalListForm);
    }

    @PostMapping("/add")
    @ApiOperation(value = "医院创建")
    @ApiOperationSupport(ignoreParameters = {"id"})
    public HospitalDTO createHospital(
            @RequestBody HospitalEditForm hospitalEditForm) {
        return hospitalService.editHospital(hospitalEditForm);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "医院修改")
    @ApiOperationSupport(ignoreParameters = {"name"})
    public HospitalDTO editHospital(
            @RequestBody HospitalEditForm hospitalEditForm) {
        return hospitalService.editHospital(hospitalEditForm);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "医院删除")
    public void deleteHospital(@PathVariable("id") Integer id) {
        hospitalService.deleteHospital(id);
    }

    // ==================================================================================================
    @PutMapping("/setting/add")
    @ApiOperation(value = "添加医院配置")
    public HospitalSettingDTO addHospitalSetting(
            @RequestBody HospitalSettingForm hospitalSettingForm) {
        return hospitalService.addHospitalSetting(hospitalSettingForm);
    }

    @GetMapping("/setting/{hospitalId}")
    @ApiOperation(value = "获取医院配置")
    public HospitalSettingDTO getHospitalSetting(
            @PathVariable(required = true, name = "hospitalId") Integer hospitalId) {
        return hospitalService.getHospitalSetting(hospitalId);
    }

    // ==================================================================================================

    @GetMapping("/{hospitalId}/department/list")
    @ApiOperation(value = "获取医院科室列表")
    List<DepartmentListDTO> getHospitalDepartmentList(@PathVariable("hospitalId") Integer hospitalId){
        HospitalDepartmentSearchFrom hospitalDepartmentSearchFrom = new HospitalDepartmentSearchFrom();
        hospitalDepartmentSearchFrom.setHospitalId(hospitalId);
        return departmentService.getHospitalDepartment(hospitalDepartmentSearchFrom);
    }

    // ==================================================================================================
    //总后台添加管理员
    @PostMapping("/admin/edit")
    @ApiOperation("医院总管理员--添加修改")
    @ApiOperationSupport(ignoreParameters = {"type","roles"})
    public HospitalAdminDTO editAdmin(@RequestBody HospitalAdminEditForm form) {
        form.setType(HospitalAdminType.admin);
        return hospitalAdminService.editAdmin(form);
    }

    @DeleteMapping("/admin/delete/{adminId}")
    @ApiOperation("医院总管理员--删除")
    public void deleteAdmin(@PathVariable Integer adminId){
        hospitalAdminService.deleteAdmin(adminId);
    }

    @GetMapping("/admin/list")
    @ApiOperation("医院总管理员--列表")
    public List<HospitalAdminListDTO> listHospitalAdmin( @RequestParam(name = "hospitalId") Integer hospitalId,
                                                         @RequestParam(name = "username",required = false) String username,
                                                         @RequestParam(name = "keyword",required = false) String keyword  ){

        HospitalAdminListForm form = HospitalAdminListForm.builder()
                                        .hospitalId(hospitalId)
                                        .username(username)
                                        .keyword(keyword)
                                        .type(HospitalAdminType.admin)
                                        .build();

        return hospitalAdminService.listHospitalAdmin(form);
    }

    @ApiOperation("获取医院热门科室")
    @GetMapping("/hot/department/{hospitalId}")
    public List<HospitalDepartmentDTO> listHotDepartment( @PathVariable Integer hospitalId){
        return departmentService.listHotDepartment(hospitalId);
    }

    private HospitalService hospitalService;

    private DepartmentService departmentService;

    private HospitalAdminService hospitalAdminService;

    @Autowired
    public void setHospitalAdminService(HospitalAdminService hospitalAdminService) {
        this.hospitalAdminService = hospitalAdminService;
    }

    @Autowired
    public void setHospitalService(HospitalService hospitalService) {
        this.hospitalService = hospitalService;
    }

    @Autowired
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }
}
